class RecorderService {

	constructor() {
		this.rc = uni.getRecorderManager();
	}
	
	init() {
		this.rc.onStart(() => {
			startTime = new Date();
		});
		this.rc.onError((e) => {
			console.log(e);
		});
		this.rc.onStop((e) => {
			console.log("录音完成", e);
			// this.upload()
		})
	}

	start() {
		return new Promise((resolve, reject) => {
			this.rc.start({
				duration: 600000,
				numberOfChannels: 1,
				sampleRate: 16000,
				format: "wav",
				frameSize: 4
			});
		})
	}

	close() {
		this.rc.stop();
	}

	upload() {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: 'http://192.168.1.22:9099/voice/aSentence',
				filePath: url,
				name: 'file',
				formData: {
					sampleRate: 16000,
					type: 'WAV',
				},
				success: (res) => {
					console.log('上传成功:', res.data);
					const uploadRes = JSON.parse(res.data);
					resolve(uploadRes);
					// const { code, message}
					// if(uploadRes.code == 200) {
					// 	resolve(data);
					// } else {
					// 	reject()
					// }
				},
				fail: (err) => {
					console.log('上传失败:', err);
					reject(err);
				}
			});
		})
	}


}

export default new RecorderService();